package com.cn.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.cn.dao.ProductCategoryDao;
import com.cn.dao.impl.ProductCategoryDaoImpl;
import com.cn.domain.Category;
import com.cn.domain.Comment;
import com.cn.domain.Product;
import com.cn.domain.User;
import com.cn.service.CommentService;
import com.cn.service.ProductCategoryService;
import com.cn.service.UserService;
import com.cn.service.impl.CommentServiceImpl;
import com.cn.service.impl.ProductCategoryServiceImpl;
import com.cn.service.impl.UserServiceImpl;
import com.cn.utils.JsonMapper;
import com.cn.utils.PageView;

@WebServlet("/PviewServlet")
public class PviewServlet extends HttpServlet {
	
	private ProductCategoryService pcsi=new ProductCategoryServiceImpl();
	
	///建立容器list用于装载用户点击的历史记录
	private LinkedList<Product> linkedList=new LinkedList();
	
	//获取json工具
	private JsonMapper binder=JsonMapper.nonDefaultMapper();
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doGet(req, resp);
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=utf-8");
		//获取到商品ID
		String pId=req.getParameter("pId");
		//根据ID后去到商品ID
		Product product=pcsi.queryProById(pId);
		//然后添加到集合中存到cookie中
		if(product!=null) {
			//添加到栈顶，判断是否已经存在	
			if(!linkedList.contains(product)) {
				//存在,啥都不用做
				linkedList.addFirst(product);
			}
			//等于5的时候就直接清空最早添加的商品
			if(linkedList!=null && (linkedList.size()==5)) {
				linkedList.removeLast();
			}
		}
		//将这个商品存到cookie中,然后在首页中就可以查出来
		String json=binder.toJson(linkedList);
		//然后存入到浏览器，这里需要进行编码和解码,否则可能会乱麻
		Cookie cookie=new Cookie("proInfo",URLEncoder.encode(json,"UTF-8"));
		//发送给用户浏览器，然后到用户浏览器中取出并展示
		cookie.setMaxAge(3600*1000*24);
		//发送给浏览器客户端
		resp.addCookie(cookie);
		
		//获取你坐在的位置
		String pid=req.getParameter("pId");
		String status=req.getParameter("cate");
		String hpcId=req.getParameter("hpcId");
		
		if(pid!=null &&hpcId!=null&&status!=null){
			//cate-->0代表的是孩子,1代表的是父亲,hpcId是表示ID号
			Product queryProById = pcsi.queryProById(pid);
			//赞根据孩子节点找父亲 0是根据HPC_ID 1是父节点
			Category queryCateById3 = pcsi.queryCateById("0",queryProById.getHpc_child_id());
			Category category = circleFunction(queryCateById3);
			//System.out.println(queryCateById3.getHpc_name()+":"+category.getHpc_name());
			req.setAttribute("queryCateById3", queryCateById3);//孩子节点名称
			req.setAttribute("category", category);//父亲节点的名称
		}
		req.setAttribute("productInfo", product);
		req.getRequestDispatcher("/product_view.jsp").forward(req, resp);
	}

	//递归出所有的父节点
	public Category circleFunction(Category queryCateById3){
		long pid=pcsi.queryCateById("0",queryCateById3.getHpc_id()).getHpc_id();
		long parentId=pcsi.queryCateById("0",queryCateById3.getHpc_parent_id()).getHpc_parent_id();
		queryCateById3=pcsi.queryCateById("0",parentId);
		if(pid==parentId){
			return queryCateById3;
		}
		//继续调用这个函数
		return circleFunction(queryCateById3);
	}
}
